package com.assessment.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.assessment.dao.ComplaintRecordRepository;
import com.assessment.entity.ComplaintRecord;

@Service
@Transactional
public class ComplaintRecordService implements IComplaintRecordService
{
	@Autowired
	private ComplaintRecordRepository complaintRecordRepository;
	@Override
	public void saveOrUpdate(ComplaintRecord complaintRecord) {
		// TODO Auto-generated method stub
		complaintRecordRepository.save(complaintRecord);
	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		ComplaintRecord complaintRecord=complaintRecordRepository.findOne(id);
		if(complaintRecord!=null) {
			complaintRecord.setStaff(null);
			complaintRecord.setAssessmentRecord(null);
			complaintRecord.setResponsiblePerson(null);		
			complaintRecordRepository.save(complaintRecord);
			complaintRecordRepository.delete(complaintRecord);
		}		
	}

	@Override
	public void delete(List<Long> ids) {
		// TODO Auto-generated method stub
		List<ComplaintRecord> complaintRecordList=(List<ComplaintRecord>) complaintRecordRepository.findAll(ids);
		if(complaintRecordList!=null) {
			for(ComplaintRecord complaintRecord :complaintRecordList) {
				complaintRecord.setStaff(null);
				complaintRecord.setAssessmentRecord(null);
				complaintRecord.setResponsiblePerson(null);
				complaintRecordRepository.save(complaintRecord);
			}
			complaintRecordRepository.delete(complaintRecordList);
		}		
	}

	@Transactional(readOnly=true)
	public ComplaintRecord findOne(Long id) {
		// TODO Auto-generated method stub
		return complaintRecordRepository.findOne(id);
	}

	@Transactional(readOnly=true)
	public List<ComplaintRecord> findAll() {
		// TODO Auto-generated method stub
		return (List<ComplaintRecord>) complaintRecordRepository.findAll();
	}

	@Transactional(readOnly=true)
	public Page<ComplaintRecord> findAll(Specification<ComplaintRecord> spec, Pageable pageable) {
		// TODO Auto-generated method stub
		return complaintRecordRepository.findAll(spec,pageable);
	}
}
